package com.infovane.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.infovane.dao.BaseDao;
import com.infovane.model.TArticleComment;
import com.infovane.service.ArticleCommentService;

public class ArticleCommentServiceImpl implements ArticleCommentService{
	public BaseDao baseDao;
	
	
	public BaseDao getBaseDao() {
		return baseDao;
	}


	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}


	/**
	 * 添加文章评论
	 * @param Objcet
	 * @return boolean
	 * */
	public boolean add(Object object) {

		return this.baseDao.save(object);
	}

	/**
	 * 根据文章ID获取评论内容
	 * @param articleId,currentPage,pageSize
	 * @return List
	 * */
	public List getArticleComment(Long articleId,int currentPage,int pageSize) {
		String hql="select c.articleCommentTitle,c.userName,c.commentCreateTime,c.articleCommentContent from TArticleComment as c where c.articleId='"+articleId+"' order by c.commentCreateTime desc";
		List list=this.baseDao.load(hql, (currentPage-1)*pageSize, pageSize);
		Iterator it=list.iterator();
		List<TArticleComment> commentList=new ArrayList<TArticleComment>();
		while(it.hasNext()){
			TArticleComment comment=new TArticleComment();
			Object[] results=(Object[]) it.next();
			comment.setArticleCommentTitle((String)results[0]);
			comment.setUserName((String)results[1]);
			comment.setCommentCreateTime((Date)results[2]);
			comment.setArticleCommentContent((String)results[3]);
			commentList.add(comment);
			
		}
		return commentList;
	}

	/**
	 * 获取文章评论记录数
	 * @param articleId
	 * @return int
	 * */
	public int getCount(Long articleId) {
		int count=0;
		String hql="select c.articleCommentId from TArticleComment as c where c.articleId="+articleId;
		List list=this.baseDao.load(hql);
		if(list.size()>0){
			count=list.size();
		}
		return count;
	}

	/**
	 * 根据用户名获取用户评论总数
	 * @param username
	 * @return int
	 * */
	public int getCount(String username) {
		int count=0;
		String hql="select c.articleCommentId from TArticleComment as c where c.userName='"+username+"'";
		List list=this.baseDao.load(hql);
		if(list.size()>0){
			count=list.size();
		}
		return count;
	}

}
